package com.hmusic.mymusic.controller;

import com.alibaba.fastjson.JSONObject;
import com.hmusic.mymusic.domain.Singer;
import com.hmusic.mymusic.domain.Song;
import com.hmusic.mymusic.service.SongService;
import com.hmusic.mymusic.utils.Consts;
import com.hmusic.mymusic.utils.DateUtil;
import com.hmusic.mymusic.utils.FileUpdateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 歌曲Controller层
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/song")
public class SongController {

    @Autowired
    private SongService songService;
    @Autowired
    private FileUpdateUtil fileUpdateUtil;
    @Value("${songPic.dir}")
    private String realPath;
    @Value("${song.dir}")
    private String songPath;

    /**
     * 添加歌曲
     */
    @PostMapping("/add")
    public Object addSong(Song song, MultipartFile mpFile){
        JSONObject jsonObject = new JSONObject();
        if (mpFile.isEmpty()){
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传歌曲文件失败");
            return jsonObject;
        }
        try{
            String pic = "/img/songPic/songPic.jpeg";
            //上传歌曲文件
            String fileName = System.currentTimeMillis()+mpFile.getOriginalFilename();
            String filePath = songPath;
            File file1 = new File(filePath);
            if (!file1.exists()){
                file1.mkdir();
            }
            //实际的文件地址
            File dest = new File(filePath+System.getProperty("file.separator")+fileName);
            //储存到数据库中的文件地址
            String storeSongPicPath = "/song/"+fileName;
            mpFile.transferTo(dest);
            if (song.getIntroduction().equals("")||song.getIntroduction().equals(null)||song.getIntroduction().equals("undefined")){
                song.setIntroduction("");
            }
            song.setPic(pic);
            song.setUrl(storeSongPicPath);
            song.setCreate_time(DateUtil.getDate());
            song.setUpdate_time(DateUtil.getDate());
            songService.insert(song);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"添加歌曲成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"添加歌曲失败");
            return jsonObject;
        }
    }

    /**
     * 根据歌手id查询歌曲
     */
    @GetMapping("/singer/detail")
    @ResponseBody
    public Object songOfSingerId(String singer_id){
        return songService.songOfSingerId(singer_id);
    }

    /**
     * 根据歌曲id查询歌曲
     */
    @GetMapping("/detail")
    @ResponseBody
    public Object songOfSongId(String songId){
        return songService.selectByPrimaryKey(songId);
    }

    /**
     * 根据歌曲名字查询歌曲
     */
    @GetMapping("/songOfName")
    @ResponseBody
    public Object songOfName(String name){
        return songService.songOfName(name);
    }

    /**
     * 修改歌曲
     */
    @PostMapping("/update")
    public Object updateSong(@RequestBody Song song){
        JSONObject jsonObject = new JSONObject();
        try{
            songService.update(song);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"修改歌曲成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"修改歌曲失败");
            return jsonObject;
        }
    }

    /**
     * 删除歌曲
     */
    @GetMapping("/delete")
    @ResponseBody
    public Object deleteSong(String id){
        JSONObject jsonObject = new JSONObject();
        try{
            songService.delete(id);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"删除歌曲成功");
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"删除歌曲失败");
            return jsonObject;
        }
    }

    /**
     * 上传/更新歌曲图片
     */
    @PostMapping("/updateSongPic")
    public Object updateSingerPic(@RequestParam("file") MultipartFile songFile,String id){
        JSONObject jsonObject = new JSONObject();
        try {
            String storeSongPath = fileUpdateUtil.updateSongPic(id,songFile);
            Song song = new Song();
            song.setId(id);
            song.setPic(storeSongPath);
            song.setUpdate_time(DateUtil.getDate());
            songService.update(song);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"上传歌曲图片成功");
            jsonObject.put("pic",storeSongPath);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"上传歌曲图片失败");
            return jsonObject;
        }
    }

    /**
     * 更新歌曲地址
     */
    @PostMapping("/updateSongUrl")
    public Object updateSingerUrl(@RequestParam("file") MultipartFile songFile,String id){
        JSONObject jsonObject = new JSONObject();
            try {
                String storeSongUrlPath = fileUpdateUtil.updateSong(id,songFile);
            Song song = new Song();
            song.setId(id);
            song.setUrl(storeSongUrlPath);
            song.setUpdate_time(DateUtil.getDate());
            songService.update(song);
            jsonObject.put(Consts.CODE,1);
            jsonObject.put(Consts.MSG,"更新歌曲地址成功");
            jsonObject.put("pic",storeSongUrlPath);
            return jsonObject;
        } catch (Exception e) {
            e.printStackTrace();
            jsonObject.put(Consts.CODE,0);
            jsonObject.put(Consts.MSG,"更新歌曲地址失败");
            return jsonObject;
        }
    }

    @GetMapping("/songCount")
    @ResponseBody
    public Object songCount(){
        return songService.songCount();
    }

}
